//
//  LFBReadBaseView.m
//  ONE
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 GroupNine. All rights reserved.
//

#import "LFBReadBaseView.h"
#import "LFBReadBaseCell.h"
#import <UITableView+FDTemplateLayoutCell.h>

NSString *const kLFBReadBaseViewID = @"LFBReadBaseViewID";

@interface LFBReadBaseView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) MASConstraint *tableViewHeightConstraint;

@end

@implementation LFBReadBaseView
{
    NSMutableArray *rowHeights;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createBaseView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createBaseView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self createBaseView];
    }
    return self;
}

- (void)createBaseView
{
    if (_scrollView) {
        return;
    }
    
    self.backgroundColor = [UIColor colorWithRed:0.98 green:0.99 blue:1.00 alpha:1.00];
    
    _scrollView = [UIScrollView new];
    _scrollView.backgroundColor = [UIColor colorWithRed:0.98 green:0.99 blue:1.00 alpha:1.00];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
    [self addSubview:_scrollView];
    [_scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(12, 12, SCREEN_WIDTH, 450)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[LFBReadBaseCell class] forCellReuseIdentifier:kLFBReadBaseCellID];
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    _tableView.scrollsToTop = NO;
    _tableView.translatesAutoresizingMaskIntoConstraints = YES;
    _tableView.layer.masksToBounds = NO;
    _tableView.layer.shadowColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1.00].CGColor;
    _tableView.layer.shadowRadius = 2;
    _tableView.layer.shadowOffset = CGSizeZero;
    _tableView.layer.shadowOpacity = 0.5;
    _tableView.layer.cornerRadius = 5;
    [_scrollView addSubview:_tableView];
    [_tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_scrollView).insets(UIEdgeInsetsMake(12, 12, 12, 12));
        make.width.equalTo(SCREEN_WIDTH - 24);
        _tableViewHeightConstraint = make.height.equalTo(450);
    }];
}

#pragma mark --UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LFBReadBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:kLFBReadBaseCellID forIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(LFBReadBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
    }
    else if (indexPath.row == 1)
    {
        
    }
    else
    {
        
    }
}

#pragma mark --UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat rowHeight = [tableView fd_heightForCellWithIdentifier:kLFBReadBaseCellID configuration:^(id cell) {
        [self configureCell:cell atIndexPath:indexPath];
    }];
    rowHeights[indexPath.row] = @(ceil(rowHeight));
    if (indexPath.row == rowHeights.count - 1) {
        NSInteger tableViewHeight = 0;
        for (NSNumber *height in rowHeights) {
            tableViewHeight += [height integerValue];
        }
        _tableViewHeightConstraint.equalTo(tableViewHeight);
    }
    
    return ceil(rowHeight);
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 10)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 10, 0, 10)];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_readSelected) {
        _readSelected(indexPath.row == 0 ? LFBReadTypeShort : (indexPath.row == 1 ? LFBReadTypeSerial : LFBReadTypeQuestion));
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
